<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class goa_json extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
		// Call the Model constructor
		parent::_construct();
	}
	function gethotellist($client,$agent,$type)
	{
		$query='select idUser from podpisannie where idUser='.$agent.' and idPodp='.$client;
		$result=$this->db->query($query);
		$responce=array();
		$hotels=array();
		if($result->num_rows>0||$agent==$client)
		{
			$query='select s.id,s.name,s.`desc`as d, o.`desc`,o.idoption
							from service as s
				            join  listoptions as o
				                on o.idservice=s.id and o.idoption in ( 13,14,15,54,55,56,57,58)
				            left join listofservices as ls on s.id=ls.idService
										where  ls.idUser='.$agent.'
											and s.idtype=1  order by s.name asc';
			$result=$this->db->query($query);
			$ids=array();
			foreach($result->result() as $row)
			{
				if(!isset($responce[$row->id]))
				{
					$responce[$row->id]['name']=$row->name;
					$responce[$row->id]['id']=$row->id;
					$responce[$row->id]['desc']=$row->d;
					$responce[$row->id]['min_p']=0;
					$responce[$row->id]['category']='';
					$responce[$row->id]['city']='';
					$responce[$row->id]['coordinates']='';
					$responce[$row->id]['dblchld']=0;
					$responce[$row->id]['chldage']=0;
					$responce[$row->id]['hotel_type']=array();
					$responce[$row->id]['rest_type']=array();
					$responce[$row->id]['distance']=0;
					$responce[$row->id]['foto']=array();
					$ids[]=$row->id;
				}
						
				switch($row->idoption)
				{
					case 13:
						$query='select id,name from service where id='.$row->desc;
						$resp=$this->db->query($query);
						if($resp->num_rows()>0)
						{
							$responce[$row->id]['city']=$resp->row()->name;
							$responce[$row->id]['idcity']=$resp->row()->id;
						}
						else
						{
							$responce[$row->id]['city']='no city';
							$responce[$row->id]['idcity']=0;
						}
					break;
					case 14:
						$responce[$row->id]['category']=$row->desc;
					break;
					case 15:
						$responce[$row->id]['coordinates']=$row->desc;
					break;
					case 54:
						$responce[$row->id]['dblchld']=$row->desc;
					break;
					case 55:
						$responce[$row->id]['chldage']=$row->desc;
					break;
					case 56:
						$responce[$row->id]['hotel_type'][]=$row->desc;
					break;
					case 57:
						$responce[$row->id]['rest_type'][]=$row->desc;
					break;
					case 58:
						$responce[$row->id]['distance']=$row->desc;
					break;
				}
					
			}
			$query="SELECT f.url,l.idservice
					FROM fotos as f
					inner join listfotos as l on l.idservice in (".implode(',',$ids).") and f.id=l.idfoto;";
			$result=$this->db->query($query);
			foreach($result->result() as $row)
			{
				//if($responce[$row->idservice]['foto']=='http://rustmarket.com/images/no.png')
				//{
					$responce[$row->idservice]['foto'][]=$row->url;
				//}
			}
		}
	
		return json_encode($responce);
	}
	function get2($dateS,$dateE,$id)
	{
		$query="Select id,name,`desc` from rows ";
		$rowsName=$this->db->query($query);
		$rowsStruct=array();
		foreach($rowsName->result() as $row)
		{
			$rowsStruct[$row->id]['name']=$row->name;
			$rowsStruct[$row->id]['desc']=$row->desc;
		}
		$responce=array();
		$responce['serviceId']=$id;
		$query="Select id,idStruct,name,`desc` from listoftables where idService=".$id;
		$result=$this->db->query($query);
		$table=0;
		$query="Select idStruct,idRow from liststruct where ";
		$str=array();
		$str2=array();
		foreach($result->result() as $row)
		{
			$idstruct=$row->idStruct;
			$tableName=$row->name;
			$idtable=$row->id;
			$responce['tables'][$idtable]['name']=$tableName;
			$responce['tables'][$idtable]['desc']=$row->desc;
			$responce['tables'][$idtable]['idStruct']=$idstruct;
			$responce['tables'][$idtable]['id']=$idtable;
			$str[]=' idStruct='.$idstruct;
			$str2[]=' idTable='.$idtable;
		}
		$query.=implode(' or ',$str);
		$result=$this->db->query($query);
		$i=0;
		foreach($result->result() as $row)
		{
			$struct[$row->idStruct][$row->idRow]['name']=$rowsStruct[$row->idRow]['name'];
			$struct[$row->idStruct][$row->idRow]['desc']=$rowsStruct[$row->idRow]['desc'];
			$i++;
		}
		$query="Select `Date`,`price`,`idRow`,idTable from price where `date`>='$dateS' and `date`<='$dateE' and";
		$query.='('.implode(' or ',$str2).')';
		$result=$this->db->query($query);
		$DATA=array();
		foreach($result->result() as $row)
		{
			$DATA[$row->idTable][$row->Date][$row->idRow]['price']=$row->price;
		}
		list($ys,$ms,$ds) = explode("-",$dateS);
		$time1 = mktime(0,0,0,$ms,$ds,$ys);
		list($ye,$me,$de) = explode("-",$dateE);
		$time2 = mktime(0,0,0,$me,$de,$ye);
		$n=($ms!=2?(($ms%2)^($ms>7))+30:(!($ys%400)||!($ys%4)&&($ys%25)?29:28));
	
	
		foreach($responce['tables'] as $idTable=>$table)
		{
			$rows=array();
			foreach($struct[$table['idStruct']] as $idRow=>$row)
			{
				$j=3;
				$responce['tables'][$idTable]['rows'][$idRow]['cell']=array($idRow,$row['name'],$row['desc']);
				for ($i = $time1; $i <= $time2; $i+=24*60*60)
				{
					$responce['tables'][$idTable]['rows'][$idRow]['cell'][$j]=0;
					$j++;
				}
			}
		}
		foreach($DATA as $idTable=>$date)
		{
			foreach($date as $md=>$row)
			{
				list($y,$m,$d) = explode("-",$md);
				$time = mktime(0,0,0,$m,$d,$y);
				$d=(int)($time-$time1)/(24*60*60);
				foreach($row as $id=>$price)
				{
					$responce['tables'][$idTable]['rows'][$id]['cell'][3+$d]=$price['price'];
				}
			}
	
		}
		return json_encode($responce);
	}
	function price($date1,$date2,$oto,$type)
	{
		$responce=array();
	
		$query='SELECT p.price as price,s.id,p.`date`
					FROM price p
					JOIN listoftables l
					     on l.id=p.idTable
					inner JOIN listofservices as l2
					     on l2.idUser='.$oto.'
					inner JOIN service as s
					     on s.id = l2.idService
					     and l.idservice=s.id
					     and s.idtype='.$type.'
					WHERE
					      p.`Date`>"'.$date1.'"
					  and p.`Date`<="'.$date2.'"
					  and p.idRow=1';

		$temp=array();
		$result=$this->db->query($query);
		foreach($result->result() as $row)
		{
			if(isset($temp[$row->id][$row->date]))
			{
				if($temp[$row->id][$row->date]>$row->price)
				$temp[$row->id][$row->date]=$row->price;
			}
			else
			{
				$temp[$row->id][$row->date]=$row->price;
			}
		}
		foreach($temp as $key=>$val)
		{
			$sum=0;
			foreach($val as $r)
			{
				$sum+=$r;
			}
			$responce[$key]=$sum*2;
		}
		return json_encode($responce);
	
	
	}
	function gethotelfull($id)
	{
		$query='select `desc` from service where id='.$id;
		$result=$this->db->query($query);
		$responce=array();
		if($result->num_rows>0)
		{
			$responce['description']=$result->row()->desc;
		}
		else
		{
			$responce['description']="No description";
		}
		return json_encode($responce);
	}
}